import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
                new Product("Laptop", 1200.0, "Electronics"),
                new Product("Mouse", 25.0, "Electronics"),
                new Product("Keyboard", 75.0, "Electronics"),
                new Product("Chair", 150.0, "Furniture"),
                new Product("Table", 250.0, "Furniture")
        );

        List<Product> expensiveProducts = products.stream()
                .filter(product -> product.getPrice() > 100)
                .toList();

        System.out.println("价格高于100的产品:\n " + expensiveProducts);

        List<String> productNames = products.stream()
                .map(product -> product.getName().toUpperCase())
                .toList();

        System.out.println("所有产品的大写名字:\n " + productNames);

        List<Product> sortedProducts = products.stream()
                .sorted((p1, p2) -> p1.getName().compareTo(p2.getName()))
                .toList();

        System.out.println("按价格排序后的产品列表:\n " + sortedProducts);

        List<String> sortedExpensiveProductNames = products.stream()
                .filter(product -> product.getPrice() > 100)
                .map(product -> product.getName().toUpperCase())
                .sorted()
                .toList();

        System.out.println("价格高于100的产品大写名字按字母顺序排序:\n " + sortedExpensiveProductNames);
        System.out.println("所有产品信息");
        products.stream()
                .forEach(System.out::println);

        long electronicCount = products.stream()
                .filter(p -> "Electronics".equals(p.getCategory()))
                .count();
                System.out.println("电子产品数量：" + electronicCount);

        Map<String, Double> productCategoryPriceMap = products.stream()
                .collect(Collectors.toMap(Product::getName, Product::getPrice));
        System.out.println("产品价格映射：\n" + productCategoryPriceMap);

        Optional<Double> total = products.stream()
                .map(Product::getPrice)
                .reduce(Double::sum);

        total.ifPresent(sum -> System.out.println("所有产品的总价：" + sum));
    }

}
